<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Fail-Safe vs Fail-Fast：并发修改策略深度解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .card-hover {
            transition: all 0.3s ease;
            border: 2px solid transparent;
        }
        
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            border-color: #667eea;
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            overflow: hidden;
            position: relative;
        }
        
        .code-header {
            background: #2d2d2d;
            padding: 8px 16px;
            font-size: 12px;
            color: #888;
            border-bottom: 1px solid #3e3e3e;
        }
        
        pre {
            margin: 0;
            padding: 20px;
            overflow-x: auto;
        }
        
        .feature-icon {
            width: 60px;
            height: 60px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 16px;
            font-size: 24px;
            margin-bottom: 16px;
        }
        
        .fail-fast-icon {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
        }
        
        .fail-safe-icon {
            background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
            color: white;
        }
        
        .comparison-table {
            background: white;
            border-radius: 16px;
            overflow: hidden;
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
        }
        
        .comparison-table th {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            font-weight: 600;
            padding: 16px;
            text-align: left;
        }
        
        .comparison-table td {
            padding: 16px;
            border-bottom: 1px solid #f0f0f0;
        }
        
        .highlight-text {
            background: linear-gradient(to right, #667eea, #764ba2);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-weight: 700;
        }
        
        .section-divider {
            height: 2px;
            background: linear-gradient(to right, transparent, #667eea, transparent);
            margin: 60px 0;
        }
        
        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        
        .animate-fade-in {
            animation: fadeInUp 0.8s ease-out;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 40px 0;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6 text-center">
            <h1 class="text-5xl font-bold mb-6 animate-fade-in">
                Fail-Safe vs Fail-Fast
            </h1>
            <p class="text-2xl mb-8 opacity-90 animate-fade-in" style="animation-delay: 0.2s">
                深入理解 Java 集合的并发修改策略
            </p>
            <div class="flex justify-center space-x-8 animate-fade-in" style="animation-delay: 0.4s">
                <div class="flex items-center">
                    <i class="fas fa-bolt text-3xl mr-3"></i>
                    <span class="text-lg">快速失败机制</span>
                </div>
                <div class="flex items-center">
                    <i class="fas fa-shield-alt text-3xl mr-3"></i>
                    <span class="text-lg">安全失败机制</span>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <!-- Introduction -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12 animate-fade-in">
            <p class="text-lg leading-relaxed text-gray-700">
                在处理集合数据结构时，<code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">fail-safe</code> 和 
                <code class="bg-purple-100 text-purple-800 px-2 py-1 rounded">fail-fast</code> 是用于描述如何处理并发修改的两种策略。
                它们各自有不同的设计目标和应用场景，理解这两种机制对于编写健壮的并发程序至关重要。
            </p>
        </div>

        <!-- Visual Comparison Diagram -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12">
            <h2 class="text-3xl font-bold mb-6 text-center highlight-text">策略对比可视化</h2>
            <div class="mermaid">
                graph TD
                    A[集合并发修改] --> B{选择策略}
                    B -->|快速检测| C[Fail-Fast]
                    B -->|容错处理| D[Fail-Safe]
                    
                    C --> E[检测到修改]
                    E --> F[抛出异常]
                    F --> G[ConcurrentModificationException]
                    
                    D --> H[创建快照]
                    H --> I[基于快照迭代]
                    I --> J[继续正常执行]
                    
                    style A fill:#f9f,stroke:#333,stroke-width:2px
                    style C fill:#f96,stroke:#333,stroke-width:2px
                    style D fill:#6cf,stroke:#333,stroke-width:2px
                    style G fill:#f66,stroke:#333,stroke-width:2px
                    style J fill:#6f6,stroke:#333,stroke-width:2px
            </div>
        </div>

        <!-- Fail-Fast Section -->
        <div class="grid md:grid-cols-2 gap-8 mb-12">
            <div class="bg-white rounded-2xl shadow-xl p-8 card-hover">
                <div class="feature-icon fail-fast-icon">
                    <i class="fas fa-exclamation-triangle"></i>
                </div>
                <h2 class="text-3xl font-bold mb-4">Fail-Fast 快速失败</h2>
                <p class="text-gray-700 mb-6 leading-relaxed">
                    Fail-fast 是一种机制，用于在集合被并发修改时，快速检测到并发修改并抛出异常。
                    这种策略的主要目标是尽早发现问题，避免在程序的后续阶段出现更复杂的错误。
                </p>
                
                <div class="space-y-4">
                    <div class="flex items-start">
                        <i class="fas fa-check-circle text-pink-500 mt-1 mr-3"></i>
                        <div>
                            <h4 class="font-semibold mb-1">快速检测</h4>
                            <p class="text-sm text-gray-600">
                                当迭代器检测到集合在迭代过程中被修改，立即抛出 ConcurrentModificationException
                            </p>
                        </div>
                    </div>
                    <div class="flex items-start">
                        <i class="fas fa-check-circle text-pink-500 mt-1 mr-3"></i>
                        <div>
                            <h4 class="font-semibold mb-1">适用场景</h4>
                            <p class="text-sm text-gray-600">
                                适用于不允许并发修改的场景，主要用于单线程中对集合的操作
                            </p>
                        </div>
                    </div>
                </div>
            </div>

            <div class="bg-white rounded-2xl shadow-xl p-8 card-hover">
                <div class="feature-icon fail-safe-icon">
                    <i class="fas fa-shield-alt"></i>
                </div>
                <h2 class="text-3xl font-bold mb-4">Fail-Safe 安全失败</h2>
                <p class="text-gray-700 mb-6 leading-relaxed">
                    Fail-safe 是一种机制，用于在集合被并发修改时，允许并发操作并在迭代时不抛出异常。
                    它的主要目标是保证集合在多线程环境中的一致性，即使集合正在被修改。
                </p>
                
                <div class="space-y-4">
                    <div class="flex items-start">
                        <i class="fas fa-check-circle text-blue-500 mt-1 mr-3"></i>
                        <div>
                            <h4 class="font-semibold mb-1">容错性</h4>
                            <p class="text-sm text-gray-600">
                                基于集合的快照进行迭代，即使集合被修改也不会抛出异常
                            </p>
                        </div>
                    </div>
                    <div class="flex items-start">
                        <i class="fas fa-check-circle text-blue-500 mt-1 mr-3"></i>
                        <div>
                            <h4 class="font-semibold mb-1">适用场景</h4>
                            <p class="text-sm text-gray-600">
                                适用于多线程环境中，集合内容可能被多个线程并发修改的情况
                            </p>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Code Examples -->
        <div class="grid md:grid-cols-2 gap-8 mb-12">
            <div>
                <h3 class="text-2xl font-bold mb-4 text-gray-800">
                    <i class="fas fa-code text-pink-500 mr-2"></i>Fail-Fast 示例
                </h3>
                <div class="code-block">
                    <div class="code-header">
                        <i class="fas fa-file-code mr-2"></i>FailFastExample.java
                    </div>
                    <pre><code>List&lt;Integer&gt; list = new ArrayList&lt;&gt;();
list.add(1);
list.add(2);
Iterator&lt;Integer&gt; iterator = list.iterator();
list.add(3); // 修改集合
while (iterator.hasNext()) {
    System.out.println(iterator.next()); 
    // 抛出 ConcurrentModificationException
}</code></pre>
                </div>
            </div>

            <div>
                <h3 class="text-2xl font-bold mb-4 text-gray-800">
                    <i class="fas fa-code text-blue-500 mr-2"></i>Fail-Safe 示例
                </h3>
                <div class="code-block">
                    <div class="code-header">
                        <i class="fas fa-file-code mr-2"></i>FailSafeExample.java
                    </div>
                    <pre><code>ConcurrentHashMap&lt;String, Integer&gt; map = 
    new ConcurrentHashMap&lt;&gt;();
map.put("A", 1);
map.put("B", 2);
for (Map.Entry&lt;String, Integer&gt; entry : 
     map.entrySet()) {
    map.put("C", 3); // 不会抛出异常
    System.out.println(entry.getKey() + 
        ": " + entry.getValue());
}</code></pre>
                </div>
            </div>
        </div>

        <!-- Comparison Table -->
        <div class="mb-12">
            <h2 class="text-3xl font-bold mb-6 text-center highlight-text">详细对比分析</h2>
            <div class="comparison-table">
                <table class="w-full">
                    <thead>
                        <tr>
                            <th class="w-1/3">特性</th>
                            <th class="w-1/3">Fail-Fast</th>
                            <th class="w-1/3">Fail-Safe</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td class="font-semibold">核心机制</td>
                            <td>快速检测并抛出异常</td>
                            <td>基于快照进行迭代</td>
                        </tr>
                        <tr>
                            <td class="font-semibold">异常处理</td>
                            <td><span class="text-red-600">ConcurrentModificationException</span></td>
                            <td><span class="text-green-600">不抛出异常</span></td>
                        </tr>
                        <tr>
                            <td class="font-semibold">性能开销</td>
                            <td>低（仅检查修改计数器）